<script setup>
import {onMounted, reactive, ref} from "vue";
import {Delete, Edit, Plus, Refresh, WarningFilled} from "@element-plus/icons-vue";
import {Search} from "@element-plus/icons-vue/global";
import {ElMessage} from "element-plus";
import {addBuilding, deleteBuilding, searchBuilding, updateBuilding} from "@/api/dormitoryApi/buildingApi.js";

const building = ref('')
const buildingId = ref('')
const addFormVisible = ref(false)
const editFormVisible = ref(false)

const clear = () => {
  building.value = '';
  search();
}

const addForm = reactive({
  name: '',
  area: '',
  people: '',
  phone: '',
})
const editForm = reactive({
  name: '',
  area: '',
  people: '',
  phone: '',
})

const edit = (id,name,area,people,phone) => {
  editFormVisible.value = true;
  buildingId.value = id;
  editForm.name = name;
  editForm.area = area;
  editForm.people = people;
  editForm.phone = phone;
}

const search = () => {
  const searchParams = {
    limit: 10,
    page: currentPage.value,
    id: building.value,
  };

  searchBuilding(searchParams).then(res => {
    if(res.status === 200){
      //查询成功
      buildings.value = res.data.buildingIdVos;
      total.value = res.data.total;
    }
  }).catch(() => {
    ElMessage.error("查询失败");
  })
}

const add = () => {
  const addParams = {
    buildingName: addForm.name,
    area: addForm.area,
    people: addForm.people,
    phone: addForm.phone,
  };

  addBuilding(addParams).then(res => {
    if(res.status === 200){
      //添加成功
      ElMessage.success("添加成功");
      addFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("添加失败");
  })
}

const update = (id,name,area,people,phone) => {
  const updateParams = {
    buildingId: id,
    buildingName: name,
    area: area,
    people: people,
    phone: phone,
  };

  updateBuilding(updateParams).then(res => {
    if(res.status === 200){
      //修改成功
      ElMessage.success("修改成功");
      editFormVisible.value = false;
      search();
    }
  }).catch(() => {
    ElMessage.error("修改失败");
  })
}

const deleteMessage = (msg) => {
  const deleteParams = {
    buildingId: msg,
  };

  deleteBuilding(deleteParams).then(res => {
    if(res.status === 200){
      //删除成功
      ElMessage.success("删除成功");
      search();
    }
  }).catch(() => {
    ElMessage.error("删除失败");
  })
}

const buildings = ref([])
const total = ref(100);
let currentPage=ref(1);

const handleCurrentChange = (val) => {
  currentPage.value = val;
  search();
}

onMounted(() => {
  search();
})
</script>

<template>
  <div class="view-container">
      <div class="header">
        <el-card>
          <span style="font-size: large; color: rgb(51,133,217);">楼栋信息管理</span>
          <div class="extra">
            <el-input v-model="building" style="width: 250px" placeholder="输入楼栋名" />
            <el-button type="primary" plain @click="search"><el-icon><Search /></el-icon>搜索楼栋</el-button>
            <el-button @click="clear"><el-icon><Refresh /></el-icon>重置</el-button>
            <el-button type="primary" @click="addFormVisible = true"><el-icon><Plus /></el-icon>添加楼栋信息</el-button>
          </div>
        </el-card>
      </div>

    <el-dialog v-model="addFormVisible" title="添加楼栋信息" width="500">
      <el-form :model="addForm">
        <el-form-item label="楼栋名">
          <el-input v-model="addForm.name" placeholder="请输入楼栋名" autocomplete="off" />
        </el-form-item>
        <el-form-item label="所属片区">
          <el-select v-model="addForm.area" placeholder="请选择片区">
            <el-option label="南校区" value="南校区" />
            <el-option label="校本部" value="校本部" />
            <el-option label="铁道校区" value="铁道校区" />
            <el-option label="湘雅校区" value="湘雅校区" />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="addForm.people" placeholder="请输入负责人" autocomplete="off" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="addForm.phone" placeholder="请输入联系电话" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addFormVisible = false">取消</el-button>
          <el-button type="primary" @click="add">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>

    <div class="table-container">
      <el-table :data="buildings" style="width: 100%">
        <el-table-column label="序号" prop="suiId" align="center" width="100"></el-table-column>
        <el-table-column label="楼栋名" prop="buildingName"></el-table-column>
        <el-table-column label="所属片区" prop="area" width="200"></el-table-column>
        <el-table-column label="楼栋负责人" prop="people" width="200"></el-table-column>
        <el-table-column label="联系电话" prop="phone" width="200"></el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template #default="{ row }">
            <el-button :icon="Edit" circle plain type="primary" @click="edit(row.buildingId,row.buildingName,row.area,row.people,row.phone)"></el-button>
            <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="WarningFilled"
                icon-color="#ffcc00"
                title="确定删除此条信息？"
                @confirm="deleteMessage(row.buildingId)"
            >
              <template #reference>
            <el-button :icon="Delete" circle plain type="danger"></el-button>
              </template>
            </el-popconfirm>
            <el-dialog v-model="editFormVisible" title="修改楼栋信息" width="500">
              <el-form :model="editForm">
                <el-form-item label="楼栋名">
                  <el-input v-model="editForm.name" placeholder="请输入楼栋名" autocomplete="off" />
                </el-form-item>
                <el-form-item label="所属片区">
                  <el-select v-model="editForm.area" placeholder="请选择片区">
                    <el-option label="南校区" value="南校区" />
                    <el-option label="校本部" value="校本部" />
                    <el-option label="铁道校区" value="铁道校区" />
                    <el-option label="湘雅校区" value="湘雅校区" />
                  </el-select>
                </el-form-item>
                <el-form-item label="负责人">
                  <el-input v-model="editForm.people" placeholder="请输入负责人" autocomplete="off" />
                </el-form-item>
                <el-form-item label="联系电话">
                  <el-input v-model="editForm.phone" placeholder="请输入联系电话" autocomplete="off" />
                </el-form-item>
              </el-form>
              <template #footer>
                <div class="dialog-footer">
                  <el-button @click="editFormVisible = false">取消</el-button>
                  <el-button type="primary" @click="update(buildingId,editForm.name,editForm.area,editForm.people,editForm.phone)">
                    确定
                  </el-button>
                </div>
              </template>
            </el-dialog>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无数据" />
        </template>
      </el-table>
    </div>

    <div class="container-bottom">
      <el-pagination
          v-model:current-page="currentPage"
          page-size="10"
          :background="true"
          layout="total, prev, pager, next"
          :total="total"
          style="margin-top: 30px; justify-content: center"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.view-container{
  overflow: hidden;
  margin-top: 30px;
  margin-left: 300px;
  margin-right: 50px;
}
.table-container{
  margin-top: 20px;
}
.extra{
  margin-top: 20px;
  display: flex;
  gap: 30px;
}
</style>
